package com.dearbinge.opentapi.content.collect;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.collect.CommodityCollectService;
import com.dearbinge.data.pojo.collect.Collect;
import com.dearbinge.data.pojo.collect.vo.CommodityCollectVO;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 收藏控制层
 *
 * @author wes
 * @date 2019/1/10
 */
@Api(tags = "2.4、商品-收藏")
@RestController
@RequestMapping("/collect")
@CrossOrigin
@EnableAutoConfiguration
public class CommodityCollectController extends WebMvcConfigurerAdapter {

    @Autowired
    private CommodityCollectService collectService;


    @ApiOperation(value = "增加修改收藏", notes = "增加修改收藏")
    @RequestMapping(value = "/insertOrUpdateCollect", method = RequestMethod.POST)
    public ResultMsg<String> insertOrUpdateCollect(@RequestBody Collect collect) {
        try {
            Integer dataCode ;
            if(collect.getId() == null || collect.getId() ==0 ){
                dataCode = collectService.insertCollect(collect);
                if (dataCode >0){
                    return ResultMsg.ok("增加成功");
                }
            }else {
                dataCode = collectService.updateCollect(collect);
                if (dataCode >0){
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    @ApiOperation(value = "根据收藏ID删除", notes = "根据收藏ID删除")
    @ApiImplicitParam(name = "collectId", value = "收藏id", paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer collectId) {
        try {
            Integer dataCode = collectService.deleteCollect(collectId);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @ApiImplicitParam(name = "id", value = "收藏表id", paramType = "query")
    @RequestMapping(value = "/getCollectById",method = RequestMethod.GET)
    public ResultMsg<Collect> getCollectById(Integer id) {
        try {
            Collect collect = collectService.getCollectById(id);
            return ResultMsg.ok(collect);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    @ApiOperation(value = "查询用户收藏夹", notes = "查询用户收藏夹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", paramType = "query", required = true),
            @ApiImplicitParam(name = "current", value = "current", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(response = CommodityCollectVO.class, code = 200, message = "返回结果参数")
    })
    @RequestMapping(value = "/listCollectByUserId", method = RequestMethod.GET)
    public ResultMsg<Page> listCollectByUserId(String userId, Integer current, Integer size) {
        try {
            Page page = collectService.listCollectByUserId(userId, current, size);
            return ResultMsg.okData(page, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



}
